package com.wlb365.financing.modules.trade.controller;

import cn.hutool.json.JSONUtil;
import com.wlb365.financing.common.constants.ApiConstants;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.common.utils.StringUtils;
import com.wlb365.financing.modules.trade.response.data.QueryInfoByKeyAndBtypeRes;
import com.wlb365.financing.modules.trade.response.data.QuerySchemaByBtypeRes;
import com.wlb365.financing.modules.trade.response.data.QuerySchemaByIdRes;
import com.wlb365.financing.modules.trade.service.IDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 数据中心服务管理类.
 *
 * @author zwh
 * @version 1.0
 */
@RestController
@RequestMapping("data")
public class DataController {
    @Autowired
    private IDataService dataService;

    /**
     * 根据模板ID获取模板.
     * @param templateId 模板ID
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @GetMapping(value = "/schemaById")
    public R querySchemaById(final String templateId) {
        QuerySchemaByIdRes res = dataService.querySchemaById(templateId);
        if (res != null && ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            return R.ok(JSONUtil.parseObj(res.getTemplateData()));
        }else{
            return R.fail("模板不存在");
        }
    }

    /**
     * 根据模板类型获取模板.
     * @param dataType 数据类型
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     * @deprecated 使用querySchemaById方法1
     **/
    @GetMapping(value = "/schemaByBtype")
    public R detailSchema(final String dataType) {
        QuerySchemaByBtypeRes res = dataService.querySchema(dataType, null);
        if (res != null && ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            return R.ok(JSONUtil.parseObj(res.getTemplateData()));
        }else{
            return R.fail("模板不存在");
        }
    }

    /**
     * 根据数据key,业务类型获取模板数据.
     * @param key 合同id
     * @param dataType 数据类型
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @GetMapping(value = "/infoBykeyAndBtype")
    public R detailSchemaInfo(final String key, final String dataType) {
        //String test = "{\"proOrder\": \"10000\",\"refNumber\": \"20000\",\"entryName\":\"民生银行\",\"bidWinningNoticeNumber\":\"200001\",\"bidWinningNoticeRoute\":\"/home/ap/nas/\",\"traAmount\":\"800000.00\",\"traSigningTime\":\"2021-04-12 12:00:00\",\"buyerId\":\"111111\",\"buyerName\":\"张三\",\"comPlayAccount\":\"play1573676\",\"sellerId\":\"999999\",\"sellerName\":\"李四\",\"comMakeAccount\":\"lisi123456\",\"projectList\":[{\"projectInfo\":{\"markNumber\":\"123\",\"markName\":\"案例名称\",\"material\":\"材质名称\",\"specificationModel\":\"规格型号\",\"meteringUnit\":\"吨\",\"quantity\":\"10\"}}],\"contractList\":[{\"contractObject\":{\"url\":[\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.tupkaeksunset.com%2Ffiles%2F5314%2F2105%2F5040%2FIMG_3660.jpg&refer=http%3A%2F%2Fwww.tupkaeksunset.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620894591&t=6352af0839663aa82d7b9ddc71ef72fa\",\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg\"]}}]}";
        QueryInfoByKeyAndBtypeRes res = dataService.querySchemaData(key, dataType);
        if (res != null && ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            return R.ok(JSONUtil.parseObj(res.getCurrentData()));
        }else{
            return R.fail("模板数据不存在");
        }
    }

    /**
    * 数据预览页.
    * @param id 业务数据id
    * @param url 业务数据获取url
    * @param templateId 模板ID
    * @author zwh
    * @return org.springframework.web.servlet.ModelAndView
    **/
    @GetMapping(value = "preview")
    public ModelAndView previewHtml(final String id, final String url, final String templateId) {
        ModelAndView mv = new ModelAndView("data/data_detail");
//        String test = "{\"contractNo\":\"100000001\",\"contractAmount\":1000.00,\"coreEnterpriseId\":\"1\",\"supplierId\":\"1\",\"projectName\":\"地在\",\"tradeDate\":\"20210201\",\"tradeTime\":\"101010\",\"processList\":[{\"contractNo\":\"100000001\",\"stepNum\":1,\"dataType\":\"contractInfo\",\"tradeId\":\"1\",\"status\":\"1\"},{\"contractNo\":\"100000001\",\"stepNum\":2,\"dataType\":\"supplyInfo\",\"tradeId\":\"1\",\"status\":\"1\"},{\"contractNo\":\"100000001\",\"stepNum\":3,\"dataType\":\"acceptanceInfo\",\"tradeId\":\"1\",\"status\":\"0\"},{\"contractNo\":\"100000001\",\"stepNum\":4,\"dataType\":\"settlementInfo\",\"tradeId\":\"1\",\"status\":\"0\"}],\"details\":[{\"stepNum\":1,\"contractNo\":\"100000001\",\"dataType\":\"contractInfo\",\"data\":null,\"status\":\"1\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:13:53\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"},{\"stepNum\":2,\"contractNo\":\"100000001\",\"dataType\":\"supplyInfo\",\"data\":null,\"status\":\"1\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:13:54\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"},{\"stepNum\":3,\"contractNo\":\"100000001\",\"dataType\":\"acceptanceInfo\",\"data\":null,\"status\":\"0\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:12:36\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"},{\"stepNum\":4,\"contractNo\":\"100000001\",\"dataType\":\"settlementInfo\",\"data\":null,\"status\":\"0\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:12:46\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"}]}";
        //交易id
        mv.addObject("id", id);
        mv.addObject("templateId", templateId);
        //拼接url请求参数
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotEmpty(id)) {
            sb.append("&id="+id);
        }
        //接接url
        StringBuffer dataUrl = new StringBuffer();
        dataUrl.append(url);
        if (sb.length() > 0) {
            dataUrl.append("?");
            dataUrl.append(sb.substring(1));
        }
        mv.addObject("dataUrl", dataUrl.toString());
        return mv;
    }
}
